package com.gfxy.modules.items.service.impl;

import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.vo.ZyqjsMemberPage;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zyqjs;
import com.gfxy.modules.items.mapper.ZyqjsMapper;
import com.gfxy.modules.items.service.IZyqjsService;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 专业群建设
 * @Author: jeecg-boot
 * @Date:   2021-11-01
 * @Version: V1.0
 */
@Service
public class ZyqjsServiceImpl extends AbstractProjectServiceImpl<ZyqjsMapper, Zyqjs> implements IZyqjsService {

	protected int memberCellNum = 4;

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Zyqjs getOneProject(Row row) {

		String date = ExcelUtil.formatCell(row.getCell(0));

		String classess  = ExcelUtil.formatCell(row.getCell(1));
		String projectName = ExcelUtil.formatCell(row.getCell(2));
		String stepName = ExcelUtil.formatCell(row.getCell(3));

		Zyqjs zyqjs = new Zyqjs();

		zyqjs.setDate(date);
		zyqjs.setClassess(translateClassess(classess));
		zyqjs.setProjectName(projectName);
		zyqjs.setStep(translateStep(stepName));
		zyqjs.setIsPass(0);
		zyqjs.setTotalScore(this.getTotalScore(zyqjs));
		return zyqjs;
	}



	@Override
	public Double getTotalScore(Zyqjs zyqjs) {

		double result = 0.0;
		int classess = zyqjs.getClassess();
		int step = zyqjs.getStep();
		if (classess == 1) {
			result = 1000;
        }else if (classess == 2) {
            result = 400;
        }else if (classess == 3) {
            result = 50;
        }
		double value = new BigDecimal(result/3).setScale(0,BigDecimal.ROUND_DOWN).doubleValue();

		if (step == 1){
			result = value;
		}else if (step == 2) {
		    result = value;
		}else if (step == 3) {
		    result -= (value*2);
		}
		return result;
	}

	private int translateClassess(String Classess){
		if(StringUtils.isEmpty(Classess)){
			return 0;
		} else if(Classess.equals("国家级")){
			return 1;
		} else if(Classess.equals("省级")){
			return 2;
		} else if(Classess.equals("校级")){
			return 3;
		} else {
			return 0;
		}
	}

	private Integer translateStep(String stepName) {
		if(stepName == null || stepName.equals("")){
			return 0;
		} else if(stepName.equals("立项")){
			return 1;
		} else if(stepName.equals("中期")){
			return 2;
		} else if(stepName.equals("验收")){
			return 3;
		} else {
			return 0;
		}
	}
	protected double computeProportion(int ranked, int totalNum){
		if(totalNum==1){
			return 1.0;
		} else if(totalNum == 2){
			if(ranked ==1){
				return 0.6;
			} else if(ranked == 2){
				return 0.4;
			} else {
				return 0.0;
			}
		} else if(totalNum == 3){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.3;
			} else if(ranked == 3){
				return 0.2;
			} else {
				return 0.0;
			}
		} else if(totalNum == 4){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.25;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else {
				return 0.0;
			}
		} else if(totalNum == 5){
			if(ranked ==1){
				return 0.45;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else if(ranked == 5){
				return 0.1;
			} else {
				return 0.0;
			}
		} else if(totalNum == 6){
			if(ranked ==1){
				return 0.45;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked == 4){
				return 0.1;
			} else if(ranked == 5){
				return 0.05;
			} else if(ranked == 6){
				return 0.05;
			}else {
				return 0.0;
			}
		} else if(totalNum >6){
			if(ranked == 1){
				return 0.4;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.15;
			} else if(ranked > 3){
				return Math.floor(0.2*10000/(totalNum-1))/10000.0 ;
			} else {
				return 0.0;
			}
		} else {
			return 0.0;
		}
	}

	@Override
	public IPage<ZyqjsMemberPage> getZyqjsMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<ZyqjsMemberPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.selectZyqjsMemberPageListByYear(date));
	}

	@Override
	public List<ZyqjsMemberPage> getZyqjsMemberPageListByYear(String date) {
		return this.baseMapper.selectZyqjsMemberPageListByYear(date);
	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Zyqjs jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(ZyqjsMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}


		TeachingResultContent stepTeachingResultContent = new TeachingResultContent();
		stepTeachingResultContent.setType(1);
		stepTeachingResultContent.setLevel(jxcg.getStep());

		try {
			stepTeachingResultContent.setValue(dictTranslater.getDictValue(ZyqjsMemberPage.class, "step", String.valueOf(jxcg.getStep())));
			result.add(stepTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}

}
